package org.association.link;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONConverter;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class LinkUtil {

    private static String ip = "120.24.22.252";

    private static String port = "8080";

    private static String url = "http://" + ip + ":" + port;


    /**
     * 添加成员
     * 0 资产注册成功   -1 资产账户已存在   -2 其他错误
     */
    public static Integer addMember(RestTemplate restTemplate, String account, Integer asset) {
        String path = url + "/asset/register?account=" + account + "&" + "asset=" + asset;
        return restTemplate.getForObject(path, Integer.class);
    }

    /**
     * 查询账户
     * {"result":true,"asset":1000}
     */
    public static Map<String, Object> getMemberAsset(RestTemplate restTemplate, String address) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/asset/select?");
        builder.append("account=").append(address);
        String res = restTemplate.getForObject(builder.toString(), String.class);
        return JSONObject.parseObject(res);
    }

    /**
     * 查询元豆账户
     * {"result":true,"asset":1000}
     */
    public static Map<String, Object> getMemberAssetWBA(RestTemplate restTemplate, String address) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/wba/balanceOf?");
        builder.append("address=").append(address);
        String res = restTemplate.getForObject(builder.toString(), String.class);
        return JSONObject.parseObject(res);
    }

    /**
     * 注入资金
     * 返回响应:0  资金注入成功   -1 资产账户不存在   -2 其他错误
     */
    public static Integer injectAsset(RestTemplate restTemplate, String address, Integer amount) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/asset/register?");
        builder.append("account=").append(address).append("&");
        builder.append("amount=").append(amount);
        return restTemplate.getForObject(builder.toString(), Integer.class);
    }


    /**
     * 注入资金
     * 返回响应:true  资金注入成功   false 其他错误
     */
    public static Boolean injectAssetWBA(RestTemplate restTemplate, String address, Integer amount) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/wba/issue?");
        builder.append("toAddress=").append(address).append("&");
        builder.append("amount=").append(amount);
        return restTemplate.getForObject(builder.toString(), Boolean.class);
    }


    /**
     * 转出资金
     * 返回响应:true
     */
    public static Boolean withdrawAssetWBA(RestTemplate restTemplate, String address, Integer amount) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/wba/destroyFrom?");
        builder.append("fromAddress=").append(address).append("&");
        builder.append("amount=").append(amount);
        return restTemplate.getForObject(builder.toString(), Boolean.class);
    }


    /**
     * 转账
     * :0  资产转移成功  -1 转移资产账户不存在   -2 接收资产账户不存在    -3 金额不足   -4 金额溢出   -5 其他错误
     */
    public static Integer transfer(RestTemplate restTemplate, String fromAddress, String toAddress,
                                   Integer amount) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/asset/transfer?");
        builder.append("fromAccount=").append(fromAddress).append("&");
        builder.append("toAccount=").append(toAddress).append("&");
        builder.append("amount=").append(amount);
        return restTemplate.getForObject(builder.toString(), Integer.class);
    }


    public static Boolean transferWBA(RestTemplate restTemplate, String fromAddress, String toAddress,
                                   Integer amount) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/wba/sendFrom?");
        builder.append("fromAddress=").append(fromAddress).append("&");
        builder.append("toAddress=").append(toAddress).append("&");
        builder.append("amount=").append(amount);
        return restTemplate.getForObject(builder.toString(), Boolean.class);
    }

    /**
     * 提出提议
     * 其中target为该提议的目标投票数
     * 返回响应: 创建的提议索引下标(即提议编号)
     */
    public static Integer addProposal(RestTemplate restTemplate, Integer target) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/vote/newProposal?");
        builder.append("target=").append(target);
        return restTemplate.getForObject(builder.toString(), Integer.class);
    }


    /**
     * 投票
     * 其中account为账户, proposalId为提议ID, isSupport为是否支持(1为支持, 其余输入为不支持)
     * 返回响应: 0 投票成功  1 提议成功
     */
    public static Integer vote(RestTemplate restTemplate, String account, Integer proposalId, Integer isSupport) {
        StringBuilder builder = new StringBuilder();
        builder.append(url).append("/vote/doVote?");
        builder.append("account=").append(account).append("&");
        builder.append("proposalId=").append(proposalId).append("&");
        builder.append("isSupport=").append(isSupport);
        return restTemplate.getForObject(builder.toString(), Integer.class);
    }

}
